﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyAspx.EasyFramework.DAL3;
using MyAspx.EasyFramework.EntityFramework;
using MyAspx.Framework.Common;
using MyAspx.Json.Conversion;

namespace WebUI.Services.Comment
{
    /// <summary>
    /// CommentHelpHandler 的摘要说明
    /// </summary>
    public class CommentHelpHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        CommentDAL _commentDal = new CommentDAL();
        public commyaspxdbEntities _commyaspxdbEntities = new commyaspxdbEntities();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            switch (action)
            {
                case "pcomment":
                    context.Response.Write(postComment(context.Request["fileid"].ToString(),context.Request["content"].ToString(),context));
                    context.Response.End();
                    break;
                //pcommentstack
                case "pcommentstack":
                    context.Response.Write(postCommentStack(context.Request["fileid"].ToString(), context.Request["content"].ToString(),int.Parse(context.Request["pid"].ToString()) , context));
                    context.Response.End();
                    break;
                    
            }
            //context.Response.Write("Hello World");
        }

        public string postComment(string fileid,string content,HttpContext context )
        {
            int guest = -1;
             guest = context.Session["myaspxuser"] != null ? 1 : 0;
             int userid = context.Session["myaspxuser"] != null ? new UserDAL().getUserid(context.Session["myaspxuser"].ToString()) : 0;
            int res= _commentDal.AddNewCodeCommentInfo(new my_comment()
            {
                codefileid = fileid,
                commentcontent = content,
                commenttime = DateTime.Now,
                guest = guest,
                userid = userid
            });
            if (res >= 1)
            {
                CommentFormat curFormat = getCommentLists(context).Last();
                return Newtonsoft.Json.JsonConvert.SerializeObject(" <div class=\"clearfix\"><input type=\"hidden\" class=\"commentid\" value=\"" + curFormat.cid + "\" /><div class=\"fl head_portrait\"><img src=\"" + curFormat.UserPic + "\" width=\"50\" height=\"50\" alt=\"\"> </div><div class=\"fl discuss_list_con\"><div class=\"discuss_list_con_tit\"><span class=\"commentusername\">" + curFormat.UserName + "</span><span class=\"commentuseraddtime\">发表于：" + curFormat.CommentAddTime + "</span></div><p>" + curFormat.Content + "</p> <div class=\"comment2\" style=\"display:none;\"><input type=\"text\" class=\"form-control\" style=\"width:400px;height:20px; \" id=\"autocom_comment2\" placeholder=\"回复消息...\" /><span class=\"label label-success btn btn-small btn-success smallformat\"  onclick=\"addnewcommentlaminate(this)\" style=\"float:right;margin-top:-20px;margin-left:5px\">回复</span><span class=\"errtip_comment2\" style=\"display: none;color:lightcoral;vertical-align:bottom;\">回复消息不可以为空！</span></div></div><div class=\"discuss_list_opa\"><span class=\"grayfont\">0个回复</span>   <button class=\"btn btn-small btn-warning badge badge-warning smallformat\" onclick=\"addnewcomment(this);return false;\" >回复</button></div></div>");
            }
            else
                return "fail";
        }

        public string postCommentStack(string fileid, string content,int pid, HttpContext context)
        {
            int guest = -1;
            guest = context.Session["myaspxuser"] != null ? 1 : 0;
            int userid = context.Session["myaspxuser"] != null ? new UserDAL().getUserid(context.Session["myaspxuser"].ToString()) : 0;
            int res = _commentDal.AddNewCodeCommentInfo(new my_comment()
            {
                codefileid = fileid,
                commentcontent = content,
                commenttime = DateTime.Now,
                guest = guest,
                userid = userid,
                pid = pid
            });
            if (res >= 1)
            {
                CommentFormat curFormat = getCommentListsStack(context).Last();
                return Newtonsoft.Json.JsonConvert.SerializeObject(" <div class=\"clearfix comment2base alert alert-success alert-dismissable\" style=\"margin-left:20px;\"><input type=\"hidden\" class=\"commentid\" value=\"" + curFormat.cid + "\" /><div class=\"fl head_portrait\"><img src=\"" + curFormat.UserPic + "\" width=\"30\" height=\"30\" style=\"width:30px;height:30px\" alt=\"\"> </div><div class=\"fl discuss_list_con\"><div class=\"discuss_list_con_tit\"><span class=\"commentusername\">" + curFormat.UserName + "</span><span class=\"commentuseraddtime2\">发表于：" + curFormat.CommentAddTime + "</span></div><p>" + curFormat.Content + "</p>" +/*+" <div class=\"comment2\" style=\"display:none;\"><input type=\"text\" class=\"form-control\" style=\"width:400px;height:20px; \" id=\"autocom_comment2\" placeholder=\"回复消息...\" /><span class=\"label label-success btn btn-small btn-success smallformat\"  onclick=\"addnewcommentlaminate(this)\" style=\"float:right;margin-top:-20px;margin-left:5px\">回复</span><span class=\"errtip_comment2\" style=\"display: none;color:lightcoral;vertical-align:bottom;\">回复消息不可以为空！</span></div>"+*/"</div>" +/*"<div class=\"discuss_list_opa\"><span class=\"grayfont\">0个回复</span>   <button class=\"btn btn-small btn-warning badge badge-warning smallformat\" onclick=\"addnewcomment(this);return false;\" >回复</button></div>"*/"</div>");
            }
            else
                return "fail";
        }
        public List<CommentFormat> getCommentLists(HttpContext context)
        {
            
                List<CommentFormat> _commentFormats = new List<CommentFormat>();
                string fiieid = context.Request["fileid"].ToString();
                //_commentFormats.Add();
                var ls = _commyaspxdbEntities.my_comment.Where(query => query.codefileid == fiieid && query.pid==null);
                //ls.ToList().ForEach(f=>new Action<bool>(delegate
                //{
                //    yield return f;
                //}));
                //ls.
                foreach (my_comment myComment in ls)
                {
                    string un = "游客";
                    string pic = "";
                    try
                    {
                        un =
                         _commyaspxdbEntities.my_users.Where(i => i.userid == myComment.userid).First().useregname;
                        pic =
                         _commyaspxdbEntities.my_users.Where(i => i.userid == myComment.userid).First().uselogopic;
                    }
                    catch (Exception)
                    {

                        un = "游客";
                        pic = "/images/avater/d1.png";
                    }
                    
                    CommentFormat curFormat = new CommentFormat()
                    {
                        CommentAddTime = (DateTime)myComment.commenttime,
                        Content = myComment.commentcontent,
                        UserName = un,
                        UserPic = pic,
                        cid = myComment.id
                    };
                    _commentFormats.Add(curFormat); ;
                }
                return _commentFormats;
            
        }
        public List<CommentFormat> getCommentListsStack(HttpContext context)
        {

            List<CommentFormat> _commentFormats = new List<CommentFormat>();
            string fiieid = context.Request["fileid"].ToString();
            var ls = _commyaspxdbEntities.my_comment.Where(query => query.codefileid == fiieid && query.pid != null);
            foreach (my_comment myComment in ls)
            {
                string un = "游客";
                string pic = "";
                try
                {
                    un =
                     _commyaspxdbEntities.my_users.Where(i => i.userid == myComment.userid).First().useregname;
                    pic =
                     _commyaspxdbEntities.my_users.Where(i => i.userid == myComment.userid).First().uselogopic;
                }
                catch (Exception)
                {

                    un = "游客";
                    pic = "/images/avater/d1.png";
                }

                CommentFormat curFormat = new CommentFormat()
                {
                    CommentAddTime = (DateTime)myComment.commenttime,
                    Content = myComment.commentcontent,
                    UserName = un,
                    UserPic = pic,
                    cid = myComment.id
                };
                _commentFormats.Add(curFormat); ;
            }
            return _commentFormats;

        } 
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}